package com.dk.springmvc.ajax.handler;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public ModelAndView handlerNullPointerException(NullPointerException e) {
        ModelAndView modelAndView = new ModelAndView("Error");
        modelAndView.addObject("eCode", HttpStatus.INTERNAL_SERVER_ERROR.value());
        modelAndView.addObject("eMsg", e.getMessage());
        return modelAndView;
    }

    /**
     * 处理所有异常
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handlerException(Exception e){
        ModelAndView modelAndView = new ModelAndView("Error");
        modelAndView.addObject("eCode", HttpStatus.INTERNAL_SERVER_ERROR.value());
        modelAndView.addObject("eMsg", e.getMessage());
        return modelAndView;
    }
}
